unit formUsernameInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, pngimage, ExtCtrls, ImgList, Math;

type
  TfrmUsernameInput = class(TForm)
    Image1: TImage;
    Label1: TLabel;
    Label2: TLabel;
    edtUsername: TEdit;
    lblInvalid: TLabel;
    ImageList1: TImageList;
    Panel1: TPanel;
    Button1: TButton;
    btnAccept: TButton;
    Bevel1: TBevel;
    procedure edtUsernameChange(Sender: TObject);
    procedure edtUsernameKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }

    class function ValidateUsername(const S: string): Boolean;
  public
    { Public declarations }

    class function Execute(var aUsername: string; const Initial: string = ''): Boolean;
  end;

var
  frmUsernameInput: TfrmUsernameInput;

implementation

{$R *.dfm}


{ TfrmUsernameInput }

procedure TfrmUsernameInput.edtUsernameChange(Sender: TObject);
begin
  if Trim(edtUsername.Text) <> '' then
  begin
    lblInvalid.Visible:= not ValidateUsername(edtUsername.Text);
    btnAccept.Enabled := not lblInvalid.Visible;
  end
  else
  begin
    lblInvalid.Visible := False;
    btnAccept.Enabled := False;
  end;

  Height := IfThen(lblInvalid.Visible, 243, 173);
end;

procedure TfrmUsernameInput.edtUsernameKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_RETURN) and (not lblInvalid.Visible) and (Trim(edtUsername.Text) <> '') then
    ModalResult := mrOk;
end;

class function TfrmUsernameInput.Execute(var aUsername: string; const Initial: string): Boolean;
begin
  with TfrmUsernameInput.Create(nil) do
  try
    if Initial <> '' then
      edtUsername.Text := Initial;

    ShowModal;

    if ModalResult = mrOk then
    begin
      aUsername := Trim(edtUsername.Text);
      Exit(True);
    end;
    
    Exit(False);
  finally
    Free;
  end;
end;

procedure TfrmUsernameInput.FormCreate(Sender: TObject);
var
  dwSize: Cardinal;
  buffer: array[byte] of char;
  StrName: string;
begin
  GetComputerName(@buffer, dwSize);
  StrName := StrPas(Buffer);

  // Kthejme emrin nga Uppercase ne
  // Sentence Case
  StrName := LowerCase(StrName);
  StrName[1] := UpCase(StrName[1]);


  edtUsername.Text := StrName;
  Height := 173;
end;

procedure TfrmUsernameInput.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_ESCAPE then
    ModalResult := mrCancel;
end;

class function TfrmUsernameInput.ValidateUsername(const S: string): Boolean;
var
  Ch: Char;
begin
  if S = '' then
    Exit(False);

  for Ch in '!@#$%&*(){}/\;''' do
    if Pos(Ch, S) > 0 then
      Exit(False);

  if Length(S) > 20 then
    Exit(False);

  if Length(S) < 3 then
    Exit(False);

  Result := True;
end;

end.
